const {
  log
} = require('debug')
const tools = require('../utils/tools')
module.exports = class user_mod extends require('./model') {
  /**
   * @description: 用户登录
   * @param {*} username
   * @param {*} password
   * @param {*} type
   * @return {*}
   */
  static LoginUser(username, password, type) {
    type = Number(type)
    return new Promise((resolve, reject) => {
      let sql = `select * from user where binary username = "${username}" and password = "${password}" && type = ${type}`

      this.query(sql).then(res => {
        resolve(res)
      }).catch(err => {
        reject("登陆失败")
      })

    })
  }
  /**
   * @description: 根据用户类型获取用户信息
   * @param {*} type
   * @param {*} currPage 页数
   * @param {*} pageNum 数量
   * @return {*}
   */
  static getUsersByTypePageMod(type, currPage, pageNum = 3) {
    pageNum = Number(pageNum)
    currPage = Number(currPage)
    const startIndex = (currPage - 1) * pageNum
    const sql = `select * from user where type = ${type} 
    order by id asc limit ${startIndex},${pageNum}`
    return new Promise((resolve, reject) => {
      this.query(sql).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }

  /**
   * @description: 获取某种类型用户的总数量
   * @param {*}
   * @return {*}
   */
  static getUsersByTypePageTotal(type) {
    return new Promise((resolve, reject) => {
      const sql = `select count(1) as count from user where type = ${type}`
      this.query(sql).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }

  /**
   * @description: 删除用户表的用户
   * @param {*} u_id
   * @return {*}
   */
  static delUserdataMod(u_id) {
    return new Promise((resolve, reject) => {
      const sql = `delete from user where id = ${u_id}`
      this.query(sql).then(res => {
        resolve("删除用户成功~~")
      }).catch(err => {
        reject("删除用户失败~~")
      })
    })
  }

  /**
   * @description: 删除阅读表用户相关数据
   * @param {*} u_id
   * @return {*}
   */
  static delRead(u_id) {
    return new Promise((resolve, reject) => {
      const sql = "delete from `read` where u_id = " + u_id
      this.query(sql).then(res => {
        resolve("删除阅读表用户成功~~")
      }).catch(err => {
        reject("删除阅读表用户失败~~")
      })
    })
  }

  /**
   * @description: 更新用户数据
   * @param {*} 
   * @return {*}
   */
  static upUserdataMod(u_id, username, password, sex, address, type) {
    let currTime = tools.getDate19()
    return new Promise((resolve, reject) => {
      let sql = `update user set username = "${username}",password = "${password}", sex = "${sex}", address = "${address}", type = ${type},update_time = "${currTime}" where id = ${u_id}`
      this.query(sql).then(res => resolve("更新成功"))
        .catch(err => reject("更新失败~~~,网络出了点小差错~~~"))
    })
  }

  /**
   * @description: 根据用户Id获取用户信息
   * @param {*} 
   * @return {*}
   */
  static getUserdataById(id) {
    let sql = `select * from user where id = ${id}`
    return new Promise((resolve, reject) => {
      this.query(sql).then(res => {
        if (res.length > 0) {
          resolve('existed')
        } else {
          resolve('notExisted')
        }
      }).catch(err => {
        reject(err)
      })
    })
  }

  // 添加用户
  static addUserDataMod(id, type, username, password, sex, address, classes) {
    const sql = `insert into user(id, type, username, password, sex, address, classes) values(
      ${id},${type},"${username}","${password}","${sex}","${address}","${classes}")`
    return new Promise(async (resolve, reject) => {
      const isExist = await this.getUserdataById(id)
      if (isExist == 'existed') resolve('用户已经存在~~~')
      else {
        this.query(sql).then(res => resolve("添加用户成功"))
          .catch(err => reject("添加用户失败~~~,网络出了点小差错~~~"))
      }

    })
  }


  /* ----------------修改个人信息--------------------- */
  //修改密码
  static updatePasswordMod(u_id, oldpassword, newpassword) {
    return new Promise((resolve, reject) => {
      let sql = "update `user` set password = ? where id = ? and password = ?"
      this.query(sql, [newpassword, u_id, oldpassword]).then(res => {
        resolve(res.affectedRows == 1 ? "修改成功" : "修改失败,可能是旧密码与新密码一致或旧密码错误")
      }).catch(err => {
        reject(err)
      })

    })
  }

  // 确认修改头像
  static updateAvatarMod(id, head) {
    return new Promise((resolve, reject) => {
      let sql = "update `user` set head = ? where id = ?"
      this.query(sql, [head, id]).then(res => {
        resolve("success")
      }).catch(err => {
        reject("fail")
      })
    })
  }
}